package com.wanhe.mealbookmodule.presenter;

import android.content.Context;


import com.wanhe.mealbookmodule.R;
import com.wanhe.mealbookmodule.presenter.views.ILoadingView;
import com.wanhe.base.util.NetUtil;
import com.wanhe.base.util.ObjectUtil;

/**
 * Created by 陈德基 on 2016/5/18.
 */
public abstract class BasePresenter {


    protected String TAG = this.getClass().getSimpleName();

    protected void loadFailed(ILoadingView reference, String msg) {
        if (checkRefrence(reference)) {
            reference.onLoadFail(msg);
            reference.onLoaded();
        }
    }

    protected void loaded(ILoadingView reference) {
        if (checkRefrence(reference)) {
            reference.onLoaded();
        }
    }

    protected boolean loading(Context context, ILoadingView reference) {
        if (checkNetWork(context, reference)) {
            if (checkRefrence(reference)) {
                reference.onLoading();
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    protected boolean checkRefrence(Object reference) {
        if (ObjectUtil.checkObject(reference)) {
            return true;
        }
        return false;
    }

    /**
     * 检测网络，如果没有网络搭配retrofit ILoadingView 接口中声明了没有网络的情况如果操作
     *
     * @param context
     */
    protected boolean checkNetWork(Context context, ILoadingView loadingView) {
        if (NetUtil.hasNetwork(context)) {
            return true;
        } else {
            if (checkRefrence(loadingView)) {
                loadFailed(loadingView, context.getString(R.string.reset_net));
            }
            return false;
        }
    }
}
